home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 15 / CU Amiga Magazine's Super CD-ROM 15 (1997)(EMAP Images)(GB)[!][issue 1997-10].iso / CUCD / Graphics / Ghostscript / source / gximage0.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-04-20  |  6.2 KB  |  202 lines

  1. /* Copyright (C) 1995, 1996, 1997 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of Aladdin Ghostscript.
  4.   
  5.   Aladdin Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author
  6.   or distributor accepts any responsibility for the consequences of using it,
  7.   or for whether it serves any particular purpose or works at all, unless he
  8.   or she says so in writing.  Refer to the Aladdin Ghostscript Free Public
  9.   License (the "License") for full details.
  10.   
  11.   Every copy of Aladdin Ghostscript must include a copy of the License,
  12.   normally in a plain ASCII text file named PUBLIC.  The License grants you
  13.   the right to copy, modify and redistribute Aladdin Ghostscript, but only
  14.   under certain conditions described in the License.  Among other things, the
  15.   License requires that the copyright notice and this notice be preserved on
  16.   all copies.
  17. */
  18.  
  19. /* gximage0.c */
  20. /* Generic image enumeration and cleanup */
  21. #include "gx.h"
  22. #include "memory_.h"
  23. #include "gserrors.h"
  24. #include "gxdevice.h"
  25. #include "gxcpath.h"
  26. #include "gximage.h"
  27.  
  28. /* Process the next piece of an image */
  29. int
  30. gx_default_image_data(gx_device *dev,
  31.   void *info, const byte **planes, int data_x, uint raster, int height)
  32. {    gx_image_enum *penum = info;
  33.     int y = penum->y;
  34.     int y_end = min(y + height, penum->rect.h);
  35.     int width_spp = penum->rect.w * penum->spp;
  36.     int nplanes = penum->num_planes;
  37.     uint bcount =            /* bytes per data row */
  38.       (width_spp / penum->num_planes * penum->bps + 7) >> 3;
  39.     fixed adjust = penum->adjust;
  40.     gx_dda_fixed_point pixel0;
  41.     ulong offset = 0;
  42.     int ignore_data_x;
  43.     int code;
  44.  
  45.     if ( height == 0 )
  46.       return 0;
  47.  
  48.     /* Set up the clipping and/or RasterOp device if needed. */
  49.  
  50.     if ( penum->clip_dev )
  51.        {    gx_device_clip *cdev = penum->clip_dev;
  52.         cdev->target = dev;
  53.         dev = (gx_device *)cdev;
  54.        }
  55.     if ( penum->rop_dev )
  56.       {    gx_device_rop_texture *rtdev = penum->rop_dev;
  57.         ((gx_device_forward *)rtdev)->target = dev;
  58.         dev = (gx_device *)rtdev;
  59.       }
  60.     pixel0 = penum->dda.pixel0;
  61.  
  62.     /* Now render complete rows. */
  63.  
  64.     for ( ; penum->y < y_end; offset += raster, penum->y++ )
  65.        {    int px;
  66.         /*
  67.          * Normally, we unpack the data into the buffer, but if
  68.          * there is only one plane and we don't need to expand the
  69.          * input samples, we may use the data directly.
  70.          */
  71.             int sourcex = data_x;
  72.         const byte *buffer =
  73.           (*penum->unpack)(penum->buffer, &sourcex,
  74.                    planes[0] + offset, data_x, bcount,
  75.                    &penum->map[0].table, penum->spread);
  76.  
  77.         for ( px = 1; px < nplanes; ++px )
  78.           (*penum->unpack)(penum->buffer + (px << penum->log2_xbytes),
  79.                    &ignore_data_x, planes[px] + offset,
  80.                    data_x, bcount,
  81.                    &penum->map[px].table, penum->spread);
  82. #ifdef DEBUG
  83.         if ( gs_debug_c('B') )
  84.           { int i, n = width_spp;
  85.             dputs("[B]row:");
  86.             for ( i = 0; i < n; i++ )
  87.               dprintf1(" %02x", buffer[i]);
  88.             dputs("\n");
  89.           }
  90. #endif
  91.         penum->cur.x = dda_current(penum->dda.row.x);
  92.         dda_next(penum->dda.row.x);
  93.         penum->cur.y = dda_current(penum->dda.row.y);
  94.         dda_next(penum->dda.row.y);
  95.         if ( !penum->interpolate )
  96.           switch ( penum->posture )
  97.             {
  98.             case image_portrait:
  99.               { /* Precompute integer y and height, */
  100.             /* and check for clipping. */
  101.             fixed yc = penum->cur.y,
  102.               yn = dda_current(penum->dda.row.y);
  103.  
  104.             if ( yn < yc )
  105.               { fixed temp = yn; yn = yc; yc = temp; }
  106.             yc -= adjust;
  107.             if ( yc >= penum->clip_outer.q.y ) goto mt;
  108.             yn += adjust;
  109.             if ( yn <= penum->clip_outer.p.y ) goto mt;
  110.             penum->yci = fixed2int_pixround(yc);
  111.             penum->hci = fixed2int_pixround(yn) - penum->yci;
  112.             if ( penum->hci == 0 ) goto mt;
  113.               }    break;
  114.             case image_landscape:
  115.               { /* Check for no pixel centers in x. */
  116.             fixed xc = penum->cur.x,
  117.               xn = dda_current(penum->dda.row.x);
  118.  
  119.             if ( xn < xc )
  120.               { fixed temp = xn; xn = xc; xc = temp; }
  121.             xc -= adjust;
  122.             if ( xc >= penum->clip_outer.q.x ) goto mt;
  123.             xn += adjust;
  124.             if ( xn <= penum->clip_outer.p.x ) goto mt;
  125.             penum->xci = fixed2int_pixround(xc);
  126.             penum->wci = fixed2int_pixround(xn) - penum->xci;
  127.             if ( penum->wci == 0 ) goto mt;
  128.               }    break;
  129.             case image_skewed:
  130.               ;
  131.             }
  132.         dda_translate(penum->dda.pixel0.x,
  133.                   penum->cur.x - penum->prev.x);
  134.         dda_translate(penum->dda.pixel0.y,
  135.                   penum->cur.y - penum->prev.y);
  136.         penum->prev = penum->cur;
  137.         code = (*penum->render)(penum, buffer, sourcex, width_spp, 1,
  138.                     dev);
  139.         if ( code < 0 )
  140.           goto err;
  141. mt:        ;
  142.        }
  143.     if ( penum->y < penum->rect.h )
  144.       { code = 0;
  145.         goto out;
  146.       }
  147.     /* End of data.  Render any left-over buffered data. */
  148.     switch ( penum->posture )
  149.       {
  150.       case image_portrait:
  151.         {    fixed yc = dda_current(penum->dda.row.y);
  152.         penum->yci = fixed2int_rounded(yc - adjust);
  153.         penum->hci = fixed2int_rounded(yc + adjust) - penum->yci;
  154.         }    break;
  155.       case image_landscape:
  156.         {    fixed xc = dda_current(penum->dda.row.x);
  157.         penum->xci = fixed2int_rounded(xc - adjust);
  158.         penum->wci = fixed2int_rounded(xc + adjust) - penum->xci;
  159.         }    break;
  160.       case image_skewed:        /* pacify compilers */
  161.         ;
  162.       }
  163.     dda_translate(penum->dda.pixel0.x, penum->cur.x - penum->prev.x);
  164.     dda_translate(penum->dda.pixel0.y, penum->cur.y - penum->prev.y);
  165.     code = (*penum->render)(penum, NULL, 0, width_spp, 0, dev);
  166.     if ( code < 0 )
  167.       { penum->y--;
  168.         goto err;
  169.       }
  170.     code = 1;
  171.     goto out;
  172. err:    /* Error or interrupt, restore original state. */
  173.     while ( penum->y > y )
  174.       { dda_previous(penum->dda.row.x);
  175.         dda_previous(penum->dda.row.y);
  176.         --(penum->y);
  177.       }
  178.     /* Note that caller must call end_image */
  179.     /* for both error and normal termination. */
  180. out:    return code;
  181. }
  182.  
  183. /* Clean up by releasing the buffers. */
  184. /* Currently we ignore draw_last. */
  185. int
  186. gx_default_end_image(gx_device *dev, void *info, bool draw_last)
  187. {    gx_image_enum *penum = info;
  188.     gs_memory_t *mem = penum->memory;
  189.     stream_IScale_state *scaler = penum->scaler;
  190.  
  191.     gs_free_object(mem, penum->rop_dev, "image RasterOp");
  192.     gs_free_object(mem, penum->clip_dev, "image clipper");
  193.     if ( scaler != 0 )
  194.       { (*s_IScale_template.release)((stream_state *)scaler);
  195.         gs_free_object(mem, scaler, "image scaler state");
  196.       }
  197.     gs_free_object(mem, penum->line, "image line");
  198.     gs_free_object(mem, penum->buffer, "image buffer");
  199.     gs_free_object(mem, penum, "gx_default_end_image");
  200.     return 0;
  201. }
  202.